/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

/**
 *
 * @author ozgencetinkaya
 */
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import javax.servlet.ServletException;
import DAO.UserDAO;
import model.UserBean;
import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;

@Controller
@RequestMapping("/signin.htm")
public class SignInController {

    @RequestMapping(method = RequestMethod.GET)
    public String redirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /* Enumeration enames; */
        HttpSession session = request.getSession(true);/*
        enames = session.getAttributeNames();
        
        if (!enames.hasMoreElements()) {
        return "signin";
        } else { */
        UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser"));
        try {
            if (currentUser.isValid()) {
                if (currentUser.getRoleId() == 1) {
                    response.sendRedirect("accountwillingwalker.htm");
                } else {
                    response.sendRedirect("accountisolatedperson.htm");
                }
            } else {
                return "signin";
            }
        } catch (NullPointerException ex) {
            return "signin";
        }
        /*}*/
        return "signin";

    }

    @RequestMapping(method = RequestMethod.POST)
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {

        try {
            UserBean user = new UserBean();					// Create an instant of the bean 'UserBean'

            user.setUserName(request.getParameter("email"));	// Get the username and password entered by the user in LoginPage.JSP
            user.setPassword(request.getParameter("password"));	// in the fields named "un" and "pw", And save them to the bean
            UserDAO userDAO = new UserDAO();
            user = userDAO.login(user);						// Call the DAO to ask the DB about the validity of this user
            // And send to it the user bean instance 
            // (which currently contains the username and password values only)
            // In case the DAO says that the user is valid, Show the JSP that welcomes the user
            // else, Show the JSP that asks the user to sign up first
            if (user.isValid()) {
                HttpSession session = request.getSession(true);	 // Create a new session instance that will be with us 
                session.setAttribute("currentSessionUser", user); // Save the bean in the session to the variable 
                if (user.getRoleId() == 1) // willing walker
                {
                    response.sendRedirect("accountwillingwalker.htm"); 		 // Call a Welcome page      		
                } else {
                    response.sendRedirect("accountisolatedperson.htm");
                }
            } else {
                response.sendRedirect("signin.htm"); 		 // Call a Please-Sign-up-fist page
            }
        } catch (Throwable theException) {
            System.out.println(theException);
        }

    }
}